import request from '@/utils/request'

// 定义通用API响应类型
interface ApiResponse<T> {
  code: number
  msg: string
  data: T
  count?: number
}

// 案件
interface LawCase {
  id: number
  title: string
  case_no: string
  customer_id: number
  main_lawyer: number
  assist_lawyer: string
  case_type: number
  case_reason: number
  status: number
  register_time: string
  register_court: string
  accept_fee: number
  service_fee: number
  risk_rate: number
  create_time: string
  update_time: string
}

// 案件节点
interface CaseNode {
  id: number
  case_id: number
  title: string
  content: string
  node_type: number
  status: number
  create_time: string
  update_time: string
}

// 获取案件列表
export function getLawCaseList(params: any): Promise<ApiResponse<LawCase[]>> {
  return request({
    url: '/api/lawcase/cases/list',
    method: 'get',
    params,
  })
}

// 获取案件详情
export function getLawCaseDetail(id: number): Promise<ApiResponse<LawCase>> {
  return request({
    url: '/api/lawcase/cases/view',
    method: 'get',
    params: { id },
  })
}

// 新增案件
export function addLawCase(data: any) {
  return request({
    url: '/api/lawcase/cases/add',
    method: 'post',
    data,
  })
}

// 编辑案件
export function editLawCase(data: any) {
  return request({
    url: '/api/lawcase/cases/edit',
    method: 'put',
    data,
  })
}

// 删除案件
export function deleteLawCase(id: number) {
  return request({
    url: '/api/lawcase/cases/del',
    method: 'delete',
    data: { id },
  })
}

// 更新案件状态
export function updateLawCaseStatus(data: { id: number; status: number }) {
  return request({
    url: '/api/lawcase/cases/update_status',
    method: 'post',
    data,
  })
}

// 获取案件节点列表
export function getCaseNodes(params: { case_id: number }): Promise<ApiResponse<CaseNode[]>> {
  return request({
    url: '/api/lawcase/node/index',
    method: 'get',
    params,
  })
}

// 新增案件节点
export function addCaseNode(data: any) {
  return request({
    url: '/api/lawcase/node/add',
    method: 'post',
    data,
  })
}

// 编辑案件节点
export function editCaseNode(data: any) {
  return request({
    url: '/api/lawcase/node/edit',
    method: 'post',
    data,
  })
}

// 删除案件节点
export function deleteCaseNode(id: number) {
  return request({
    url: '/api/lawcase/node/delete',
    method: 'post',
    data: { id },
  })
}

// 更新案件节点状态
export function updateCaseNodeStatus(data: { id: number; status: number }) {
  return request({
    url: '/api/lawcase/node/update_status',
    method: 'post',
    data,
  })
}

// 获取案件类型列表
export function getCaseTypes(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/lawcase/casetype/index',
    method: 'get',
    params,
  })
}

// 获取案件案由列表
export function getCaseReasons(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/lawcase/casereason/index',
    method: 'get',
    params,
  })
}

// 获取节点类型列表
export function getNodeTypes(params: any): Promise<ApiResponse<any[]>> {
  return request({
    url: '/api/lawcase/nodetype/index',
    method: 'get',
    params,
  })
}

// 删除案件文档
export function deleteCaseDoc(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/lawcase/casedoc/delete',
    method: 'post',
    data: { id },
  })
}

// 上传案件文档
export function uploadCaseDoc(data: FormData): Promise<ApiResponse<any>> {
  return request({
    url: '/api/lawcase/casedocs/add',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}

// 下载案件文档
export function downloadCaseDoc(fileId: number): Promise<void> {
  return request({
    url: '/api/lawcase/casedocs/download',
    method: 'get',
    params: { file_id: fileId },
    responseType: 'blob',
  })
}

// 查看案件文档
export function viewCaseDoc(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/lawcase/casedocs/view',
    method: 'get',
    params: { id },
  })
}

// 获取案件进度列表
export function getProgressList(params: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/lawcase/progresses/list',
    method: 'get',
    params,
  })
}
